extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/z/OptionButton
onready var word_map_op = $PanelContainer/VBoxContainer/word_map/SearchSelect
onready var dir_op = $PanelContainer/VBoxContainer/dir/RoleDirSelect
onready var name_input = $PanelContainer/VBoxContainer/name/LineEdit

var edit_data = null



func set_edit_data(data):
	edit_data = data
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	var door_list = DataSource.get_data_list(DataSource.map_door)
	
	word_map_op.set_data("选择世界地图位置", FlyMap.get_op_list())
	
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["z"]))
		word_map_op.set_value(data["worldMapId"])
		dir_op.select(dir_op.get_item_index(data["dir"]))
		
		name_input.text = data["description"]
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑恢复中心"

	else:
		pass
		

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	
	
	if edit_data:
		read_form(edit_data)
		if !edit_data["description"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.update_data(DataSource.map_center_recover, edit_data)
		
		MainTool.toast_info("更新恢复中心成功!")
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"mapId": position_info["map_register"]["id"],
			# 默认状态1
			"state": 1,
		}
		
		read_form(data)
		
		if !data["description"]: 
			MainTool.toast_info("名称不能为空!")
			return
	
		var pre_list = DataSource.get_data_by_list(DataSource.map_center_recover, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_center_recover, data)
		
		MainTool.toast_info("添加恢复中心成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()

func read_form(dic):
	dic["z"] = z_op.get_selected_id()
	dic["description"] = name_input.text
	dic["worldMapId"] = word_map_op.get_value()
	dic["dir"] = dir_op.get_selected_id()
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	dic["mapId"] = position_info["map_register"]["id"]

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
